package com.tmall.tmall.controller;

import com.tmall.tmall.pojo.*;
import com.tmall.tmall.service.OrderService;
import com.tmall.tmall.service.ShoppingcartService;
import com.tmall.tmall.utils.ThreadLocalUtils;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.List;

@RestController
@RequestMapping("/shoppingCart")
public class ShoppingCartController {
    private static final Logger log = LogManager.getLogger(ShoppingCartController.class);
    @Autowired
    private ShoppingcartService shoppingcartService;



    @PostMapping("/add")
    public Result addShoppingCart(@RequestBody Shoppingcart shoppingCart) {
        Integer uid = ThreadLocalUtils.getUid();
        shoppingCart.setUserId(Long.valueOf(uid));
        shoppingcartService.save(shoppingCart);
        return Result.success();
    }

    @GetMapping("/page")
    public Result getShoppingCartPage(@RequestParam(defaultValue = "1") Integer pageNum,
                                      @RequestParam(defaultValue = "10") Integer pageSize) {

        PageRt list =  shoppingcartService.page(pageNum,pageSize);


        return Result.success(list);
    }

    @DeleteMapping("/delBatch")
    public Result delBatch(@RequestBody List<Long> ids) {

        shoppingcartService.deleateShoppingCart(ids);

        return Result.success();
    }

    @PutMapping("/update")
    public Result updateShoppingCart(@RequestBody Shoppingcart shoppingCart) {

        shoppingcartService.updateQuantity(shoppingCart);

        return Result.success();
    }

    @PostMapping("/createOrder")
    public Result createOrder(@RequestBody ShoppingCartSettlementDTO settlementDTO) {
        shoppingcartService.insertOrder(settlementDTO);

        return Result.success();
    }


}
